home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / indentsr / args.c next >
C/C++ Source or Header  |  1990-01-04  |  9KB  |  287 lines

  1. /*
  2.  * Copyright (c) 1985 Sun Microsystems, Inc.
  3.  * Copyright (c) 1980 The Regents of the University of California.
  4.  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms are permitted
  8.  * provided that the above copyright notice and this paragraph are
  9.  * duplicated in all such forms and that any documentation,
  10.  * advertising materials, and other materials related to such
  11.  * distribution and use acknowledge that the software was developed
  12.  * by the University of California, Berkeley, the University of Illinois,
  13.  * Urbana, and Sun Microsystems, Inc.  The name of either University
  14.  * or Sun Microsystems may not be used to endorse or promote products
  15.  * derived from this software without specific prior written permission.
  16.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  17.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  18.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19.  */
  20.  
  21. #ifndef lint
  22. static char sccsid[] = "@(#)args.c    5.6 (Berkeley) 9/15/88";
  23. #endif /* not lint */
  24.  
  25. /*
  26.  * Argument scanning and profile reading code.  Default parameters are set
  27.  * here as well.
  28.  */
  29.  
  30. #include "indent_globs.h"
  31. #include <sys/types.h>
  32. #include <ctype.h>
  33.  
  34. char       *getenv(), *index();
  35.  
  36. /* profile types */
  37. #define    PRO_SPECIAL    1    /* special case */
  38. #define    PRO_BOOL    2    /* boolean */
  39. #define    PRO_INT        3    /* integer */
  40. #define PRO_FONT    4    /* troff font */
  41.  
  42. /* profile specials for booleans */
  43. #define    ON        1    /* turn it on */
  44. #define    OFF        0    /* turn it off */
  45.  
  46. /* profile specials for specials */
  47. #define    IGN        1    /* ignore it */
  48. #define    CLI        2    /* case label indent (float) */
  49. #define    STDIN        3    /* use stdin */
  50. #define    KEY        4    /* type (keyword) */
  51.  
  52. /*
  53.  * N.B.: because of the way the table here is scanned, options whose names are
  54.  * substrings of other options must occur later; that is, with -lp vs -l, -lp
  55.  * must be first.  Also, while (most) booleans occur more than once, the last
  56.  * default value is the one actually assigned.
  57.  */
  58. struct pro {
  59.     char       *p_name;        /* name, eg -bl, -cli */
  60.     int         p_type;        /* type (int, bool, special) */
  61.     int         p_default;    /* the default value (if int) */
  62.     int         p_special;    /* depends on type */
  63.     int        *p_obj;        /* the associated variable */
  64. }           pro[] = {
  65.  
  66.     "T", PRO_SPECIAL, 0, KEY, 0,
  67.     "bacc", PRO_BOOL, false, ON, &blanklines_around_conditional_compilation,
  68.     "badp", PRO_BOOL, false, ON, &blanklines_after_declarations_at_proctop,
  69.     "bad", PRO_BOOL, false, ON, &blanklines_after_declarations,
  70.     "bap", PRO_BOOL, false, ON, &blanklines_after_procs,
  71.     "bbb", PRO_BOOL, false, ON, &blanklines_before_blockcomments,
  72.     "bc", PRO_BOOL, true, OFF, &ps.leave_comma,
  73.     "bl", PRO_BOOL, true, OFF, &btype_2,
  74.     "br", PRO_BOOL, true, ON, &btype_2,
  75.     "bs", PRO_BOOL, false, ON, &Bill_Shannon,
  76.     "cdb", PRO_BOOL, true, ON, &comment_delimiter_on_blankline,
  77.     "cd", PRO_INT, 0, 0, &ps.decl_com_ind,
  78.     "ce", PRO_BOOL, true, ON, &cuddle_else,
  79.     "ci", PRO_INT, 0, 0, &continuation_indent,
  80.     "cli", PRO_SPECIAL, 0, CLI, 0,
  81.     "c", PRO_INT, 33, 0, &ps.com_ind,
  82.     "di", PRO_INT, 16, 0, &ps.decl_indent,
  83.     "dj", PRO_BOOL, false, ON, &ps.ljust_decl,
  84.     "d", PRO_INT, 0, 0, &ps.unindent_displace,
  85.     "eei", PRO_BOOL, false, ON, &extra_expression_indent,
  86.     "ei", PRO_BOOL, true, ON, &ps.else_if,
  87.     "fbc", PRO_FONT, 0, 0, (int *) &blkcomf,
  88.     "fbx", PRO_FONT, 0, 0, (int *) &boxcomf,
  89.     "fb", PRO_FONT, 0, 0, (int *) &bodyf,
  90.     "fc1", PRO_BOOL, true, ON, &format_col1_comments,
  91.     "fc", PRO_FONT, 0, 0, (int *) &scomf,
  92.     "fk", PRO_FONT, 0, 0, (int *) &keywordf,
  93.     "fs", PRO_FONT, 0, 0, (int *) &stringf,
  94.     "ip", PRO_BOOL, true, ON, &ps.indent_parameters,
  95.     "i", PRO_INT, 8, 0, &ps.ind_size,
  96.     "lc", PRO_INT, 0, 0, &block_comment_max_col,
  97.     "lp", PRO_BOOL, true, ON, &lineup_to_parens,
  98.     "l", PRO_INT, 78, 0, &max_col,
  99.     "nbacc", PRO_BOOL, false, OFF, &blanklines_around_conditional_compilation,
  100.     "nbadp", PRO_BOOL, false, OFF, &blanklines_after_declarations_at_proctop,
  101.     "nbad", PRO_BOOL, false, OFF, &blanklines_after_declarations,
  102.     "nbap", PRO_BOOL, false, OFF, &blanklines_after_procs,
  103.     "nbbb", PRO_BOOL, false, OFF, &blanklines_before_blockcomments,
  104.     "nbc", PRO_BOOL, true, ON, &ps.leave_comma,
  105.     "nbs", PRO_BOOL, false, OFF, &Bill_Shannon,
  106.     "ncdb", PRO_BOOL, true, OFF, &comment_delimiter_on_blankline,
  107.     "nce", PRO_BOOL, true, OFF, &cuddle_else,
  108.     "ndj", PRO_BOOL, false, OFF, &ps.ljust_decl,
  109.     "neei", PRO_BOOL, false, OFF, &extra_expression_indent,
  110.     "nei", PRO_BOOL, true, OFF, &ps.else_if,
  111.     "nfc1", PRO_BOOL, true, OFF, &format_col1_comments,
  112.     "nip", PRO_BOOL, true, OFF, &ps.indent_parameters,
  113.     "nlp", PRO_BOOL, true, OFF, &lineup_to_parens,
  114.     "npcs", PRO_BOOL, false, OFF, &proc_calls_space,
  115.     "npro", PRO_SPECIAL, 0, IGN, 0,
  116.     "npsl", PRO_BOOL, true, OFF, &procnames_start_line,
  117.     "nps", PRO_BOOL, false, OFF, &pointer_as_binop,
  118.     "nsc", PRO_BOOL, true, OFF, &star_comment_cont,
  119.     "nsob", PRO_BOOL, false, OFF, &swallow_optional_blanklines,
  120.     "nv", PRO_BOOL, false, OFF, &verbose,
  121.     "pcs", PRO_BOOL, false, ON, &proc_calls_space,
  122.     "psl", PRO_BOOL, true, ON, &procnames_start_line,
  123.     "ps", PRO_BOOL, false, ON, &pointer_as_binop,
  124.     "sc", PRO_BOOL, true, ON, &star_comment_cont,
  125.     "sob", PRO_BOOL, false, ON, &swallow_optional_blanklines,
  126.     "st", PRO_SPECIAL, 0, STDIN, 0,
  127.     "troff", PRO_BOOL, false, ON, &troff,
  128.     "v", PRO_BOOL, false, ON, &verbose,
  129.     /* whew! */
  130.     0, 0, 0, 0, 0
  131. };
  132.  
  133. /*
  134.  * set_profile reads $HOME/.indent.pro and ./.indent.pro and handles arguments
  135.  * given in these files.
  136.  */
  137. set_profile()
  138. {
  139.     register FILE *f;
  140.     char        fname[BUFSIZ];
  141. #ifdef MSDOS
  142.     static char prof[] = "indent.pro";
  143. #else
  144.     static char prof[] = ".indent.pro";
  145. #endif
  146.  
  147.     sprintf(fname, "%s/%s", getenv("HOME"), prof);
  148.     if ((f = fopen(fname, "r")) != NULL) {
  149.     scan_profile(f);
  150.     (void) fclose(f);
  151.     }
  152.     if ((f = fopen(prof, "r")) != NULL) {
  153.     scan_profile(f);
  154.     (void) fclose(f);
  155.     }
  156. }
  157.  
  158. scan_profile(f)
  159.     register FILE *f;
  160. {
  161.     register int i;
  162.     register char *p;
  163.     char        buf[BUFSIZ];
  164.  
  165.     while (1) {
  166.     for (p = buf; (i = getc(f)) != EOF && (*p = i) > ' '; ++p);
  167.     if (p != buf) {
  168.         *p++ = 0;
  169.         if (verbose)
  170.         printf("profile: %s\n", buf);
  171.         set_option(buf);
  172.     }
  173.     else if (i == EOF)
  174.         return;
  175.     }
  176. }
  177.  
  178. char       *param_start;
  179.  
  180. eqin(s1, s2)
  181.     register char *s1;
  182.     register char *s2;
  183. {
  184.     while (*s1) {
  185.     if (*s1++ != *s2++)
  186.         return (false);
  187.     }
  188.     param_start = s2;
  189.     return (true);
  190. }
  191.  
  192. /*
  193.  * Set the defaults.
  194.  */
  195. set_defaults()
  196. {
  197.     register struct pro *p;
  198.  
  199.     /*
  200.      * Because ps.case_indent is a float, we can't initialize it from the
  201.      * table:
  202.      */
  203.     ps.case_indent = 0.0;    /* -cli0.0 */
  204.     for (p = pro; p->p_name; p++)
  205.     if (p->p_type != PRO_SPECIAL && p->p_type != PRO_FONT)
  206.         *p->p_obj = p->p_default;
  207. }
  208.  
  209. set_option(arg)
  210.     register char *arg;
  211. {
  212.     register struct pro *p;
  213.     extern double atof();
  214.  
  215.     arg++;            /* ignore leading "-" */
  216.     for (p = pro; p->p_name; p++)
  217.     if (*p->p_name == *arg && eqin(p->p_name, arg))
  218.         goto found;
  219.     fprintf(stderr, "indent: unknown parameter \"%s\"\n", arg - 1);
  220.     exit(1);
  221. found:
  222.     switch (p->p_type) {
  223.  
  224.     case PRO_SPECIAL:
  225.     switch (p->p_special) {
  226.  
  227.     case IGN:
  228.         break;
  229.  
  230.     case CLI:
  231.         if (*param_start == 0)
  232.         goto need_param;
  233.         ps.case_indent = atof(param_start);
  234.         break;
  235.  
  236.     case STDIN:
  237.         if (input == 0)
  238.         input = stdin;
  239.         if (output == 0)
  240.         output = stdout;
  241.         break;
  242.  
  243.     case KEY:
  244.         if (*param_start == 0)
  245.         goto need_param;
  246.         {
  247.         register char *str = (char *) malloc(strlen(param_start) + 1);
  248.         strcpy(str, param_start);
  249.         addkey(str, 4);
  250.         }
  251.         break;
  252.  
  253.     default:
  254.         fprintf(stderr, "\
  255. indent: set_option: internal error: p_special %d\n", p->p_special);
  256.         exit(1);
  257.     }
  258.     break;
  259.  
  260.     case PRO_BOOL:
  261.     if (p->p_special == OFF)
  262.         *p->p_obj = false;
  263.     else
  264.         *p->p_obj = true;
  265.     break;
  266.  
  267.     case PRO_INT:
  268.     if (*param_start == 0) {
  269.     need_param:
  270.         fprintf(stderr, "indent: ``%s'' requires a parameter\n",
  271.             arg - 1);
  272.         exit(1);
  273.     }
  274.     *p->p_obj = atoi(param_start);
  275.     break;
  276.  
  277.     case PRO_FONT:
  278.     parsefont((struct fstate *) p->p_obj, param_start);
  279.     break;
  280.  
  281.     default:
  282.     fprintf(stderr, "indent: set_option: internal error: p_type %d\n",
  283.         p->p_type);
  284.     exit(1);
  285.     }
  286. }
  287.